package linearlist;

/**
 * 双链表
 * @author zhm
 * @date 2020/9/25
 */
public class MyDoubleLinkedList {

    /**
     *   头结点
     */
    private  Node first;

    /**
     *   尾结点
     */
    private  Node last;

    private  int size;

    public MyDoubleLinkedList(){

    }

    private static class Node{

        Node prev;
        Node next;
        int val;

        Node(int val,Node prev,Node next){
            this.prev = prev;
            this.next = next;
            this.val = val;
        }
    }

    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    public int get(int index) {
        if(index<0 || index>=size){
            return -1;
        }
        //index 小于链表长度的一半，从头节点开始遍历；否则从尾节点开始遍历
        if(index < (size>>1)){
            Node temp = first;
            for (int i = 0; i < size; i++) {
                if(i==index){
                    return temp.val;
                }
                temp = temp.next;
            }
        }else {
            Node temp = last;
            for (int i=size-1;i>=0;i--){
                if(i==index){
                    return temp.val;
                }
                temp = temp.prev;
            }
        }
        return -1;
    }

    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    public void addAtHead(int val) {
        Node node = new Node(val,null,null);
        if(first==null){
            first = node;
            last = node;
        }else {
            node.next = first;
            first.prev = node;
            first = node;
        }
        size++;
    }

    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        Node node = new Node(val, null, null);
        if(last==null){
            first = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = last.next;
        }
        size++;
    }

    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    public void addAtIndex(int index, int val) {
        if(index<0 || index>size){
            return;
        }
        if(index==0){
            this.addAtHead(val);
        }else if(index==size){
            this.addAtTail(val);
        }else {
            if(index<(size<<1)){
                Node temp = first;
                for (int i = 0; i < size; i++) {
                    if(i==index-1){
                        Node node = new Node(val,null,null);
                        temp.next.prev = node;
                        node.next = temp.next;
                        temp.next = node;
                        node.prev = temp;
                    }
                    temp = temp.next;
                }
            }else {
                Node temp = last;
                for (int i = size-1;i>=0;i--){
                    if(i==index){
                        Node node = new Node(val,null,null);
                        temp.prev.next = node;
                        node.prev = temp.prev;
                        node.next = temp;
                        temp.prev = node;
                    }
                    temp = temp.prev;
                }
            }
            size++;
        }

    }

    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {

        if(index<0 || index>=size){
            return;
        }
        if(size==1){
            first = null;
            last = null;
        }else {
            //头部开始扫描进行删除
            if(index<(size>>1)){
                Node temp = first;
                for (int i = 0; i < size; i++) {
                    if(i==index){
                        //判断是否是头结点
                        if(temp.prev==null){
                            temp.next.prev = null;
                            first = temp.next;
                        }else {
                            //判断是不是尾结点
                            if(temp.next==null){
                                temp.prev.next = null;
                                last = temp.prev;
                            }else {
                                temp.prev.next = temp.next;
                                temp.next.prev = temp.prev;
                            }
                        }
                    }
                    temp = temp.next;
                }
            }else {
                //尾部扫描进行删除
                Node temp = last;
                for (int i = size-1;i>=0;i--){
                    if(i == index){
                        //判断是否是尾结点
                        if(temp.next==null){
                            temp.prev.next = null;
                            last = temp.prev;
                        }else {
                            temp.prev.next = temp.next;
                            temp.next.prev = temp.prev;
                        }
                    }
                    temp = temp.prev;
                }
            }
        }
        size--;
    }

}
